package controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.AppCategory;
import pojo.AppInfo;
import service.AppInfoService;
import tool.PageSupport;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;


@Controller
public class AppInfoController {
    @Resource
    private AppInfoService appInfoService;

    @RequestMapping(value="/appcategory",produces="text/html;charset=UTF-8"


    )
    @ResponseBody
    public String appcategorylist(Integer ids) {

        List<AppCategory> appCategorylists = appInfoService.appCategoryLists(ids);

        return JSONArray.toJSONString(appCategorylists);
    }

    //删除基础信息表信息
    @RequestMapping("/deleteAppInfo")
    @ResponseBody
    public int deleteAppInfo(int inId) {

        int deteid = appInfoService.deleteAppInfo(inId);
        return deteid;
    }
    @RequestMapping("/AppInfoLowerShelf")
    @ResponseBody
    public int AppInfoLowerShelf(int inId) {
        int UpdateLowerShelf = appInfoService.AppInfoLowerShelf(inId);

        return UpdateLowerShelf;
    }

    @RequestMapping(value="/AppInfoList",produces="text/html;charset=UTF-8")
    @ResponseBody
    public String appInfoList(@RequestParam(value = "pageNo", required = false) Integer pageNo, AppInfo appInfo
    ) {
        HashMap<String, Object> appInfosMap = new HashMap<String, Object>();

        PageSupport pages = new PageSupport();
        //总数量（表）
        int totalCount = appInfoService.appListCount(appInfo);

        //设置页面容量
        int pageSize = 5;
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        //  int totalPageCount = pages.getTotalPageCount();
        int pageNos = 0;
        if (pageNo == null) {
            pageNo = 1;
        }
        pageNos = (pageNo - 1) * pageSize;
        pages.setCurrentPageNo(pageNos);
        List<AppInfo> AppInfoList = appInfoService.appInfoList(appInfo,pageNos,pageSize);
        appInfosMap.put("AppInfoList", AppInfoList);
        appInfosMap.put("pages", pages);

        String appInfoLists = JSONObject.toJSONString(appInfosMap);

        return appInfoLists;
    }

    //添加APP基础信息
    @RequestMapping("/AddAppInfo")
    @ResponseBody
    public int AddAppInfo(AppInfo appInfo) {

        String thps = appInfo.getLogoPicPath();
       int addindex=appInfoService.AddAppInfo(appInfo);

        return addindex;
    }
    //根据ID查询App信息
    @RequestMapping("/AppInfoId")
    public String AppInfoId(Integer noid, Model model){
        if(noid==null){
            model.addAttribute("staff","AddAppInfo");
            return "index";
        }if(noid==-1){
            model.addAttribute("staff","index");
            return "index";
        }
        AppInfo appInfo=new AppInfo();
        appInfo=appInfoService.appInfoListId(noid);
        model.addAttribute("appInfo",appInfo);
        model.addAttribute("staff","update");
     //   model.addAttribute("status",appInfoService.DataDictionaryList("APP_STATUS"));
      //  model.addAttribute("appCategorylist",appInfoService.appCategoryList(null));
      //  model.addAttribute("flatforms",appInfoService.DataDictionaryList("APP_FLATFORM"));
       //if判断不能改变位置
        if(noid==null){
            model.addAttribute("staff","AddAppInfo");
            return "index";
        }if(noid==-1){
            model.addAttribute("staff","index");
            return "index";
        }
        model.addAttribute("getCategoryLevel2",appInfoService.appCategoryLists(appInfo.getCategoryLevel1()));
        model.addAttribute("getCategoryLevel3",appInfoService.appCategoryLists(appInfo.getCategoryLevel2()));

        return "index";
    }
    //添加APP基础信息
    @RequestMapping("/updateAppInfo")
    @ResponseBody
    public int updateAppInfo(AppInfo appInfo, @RequestParam(value = "appinfoid", required = false) Integer appinfoid) {
                int status= appInfo.getStatus();
                //判断App状态id如果审核未通过，改为待审核状态id
        if(appinfoid==0){
                if(status==3){
                    appInfo.setStatus(1);
                }}
        int addindex=appInfoService.UpdateAppInfo(appInfo);
        return addindex;
    }

}
